var pageParam = {grids: {}, trees: {}, buttons: {}, operations: {}, queryFields: {}, fields: {}, data: {}};
var moduleMap = {};
var mainMode = {};
var layoutSize = 0;
var load = null;

$(function() {
	load = baseJs.loadding();
	$(document.body).empty();
	var code = baseJs.request("code");
	$.post(host + "showAction/initPage.html", {code: code}, function(data) {
		try {
			if (data.status == "error") {
				alert(data.msg);
			} else {
				moduleMap = data;
				for (var key in moduleMap) {
					if (moduleMap[key].type == "edit" || moduleMap[key].type == "list" || moduleMap[key].type == "tree") {
						layoutSize ++;
					}
				}
				initPageLayout();
				if (layoutSize > 1) {
					$(document.body).addClass("easyui-layout");
				}
				$.parser.parse();
			}
		} catch (e) {
			//当页面首次初始化错误时，重新加载页面
			if (!top.window.initPageError) {
				top.window.initPageError = true;
				window.location.reload();
			} else {
				alert("初始化页面失败：" + e.message);
			}
		}
		load.close();
	}, "json");
});
//初始化页面布局
function initPageLayout() {
	var code = baseJs.request("code");
	for (var key in moduleMap) {
		if (code == key) {
			mainMode = moduleMap[key];
		}
		if (moduleMap[key].type == "edit") {
			initEditPanel(moduleMap[key]);
		} else if (moduleMap[key].type == "list") {
			initDagaGrid(moduleMap[key]);
		} else if (moduleMap[key].type == "tree") {
			initTree(moduleMap[key]);
		}
	}
}
var pageLayout = {};
//创建布局
var layoutBorder = {
	north: 'border-button: 1px #b0d0f0 solid;',
	center: "",
	west: "border-right: 1px #b0d0f0 solid;",
	east: "border-left: 1px #b0d0f0 solid;",
	south: "border-top: 1px #b0d0f0 solid;"
};
function createLayout(json) {
	//如果页面只有一个布局，不使用布局
	if (layoutSize == 1) {
		return $(document.body);
	}
	if (pageLayout[json.layout] == undefined) {
		var style = "";
		if (json.width > 1) {
			style += "width: " + json.width + "px;";
		}
		if (json.height > 1) {
			style += "height: " + json.height + "px;";
		}
		style += layoutBorder[json.layout];
		pageLayout[json.layout] = $('<div region="' + json.layout + '" border="false" split="true" style="' + style + '"></div>');
		$(document.body).append(pageLayout[json.layout]);
	}
	return pageLayout[json.layout];
}
function getGrid(code) {
	return pageParam.grids[code];
}
function getTree(code) {
	return pageParam.trees[code];
}
function getFields() {
	return pageParam.fields[code];
}
function getInput(code, fieldName) {
	return pageParam.fields[code];
}